/*DONE* Table [dbo].[<CompanyName>$Item Status Link$5ecfc871-5d82-43f1-9c54-59685e82318d]  1/1  Script Date: 28.7.2020 - JI ******/
/*Add prestaging table for one specific company*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyID as INT

SET @CompanyID = X --The X is the value that needs to be replaced with the [Company] ID from LSInsight$Companies
SET @Company = (SELECT LSInsight$Companies.[CompanyPrefix] from dbo.LSInsight$Companies WHERE [Company] = @CompanyID)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].['+@Company+'$Item Status Link$5ecfc871-5d82-43f1-9c54-59685e82318d]'
EXEC(@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].['+@Company+'$Item Status Link$5ecfc871-5d82-43f1-9c54-59685e82318d](
	[timestamp] [timestamp] NOT NULL,
	[Item No_] [nvarchar](20) NOT NULL,
	[Variant Dimension 1 Code] [nvarchar](15) NOT NULL,
	[Variant Code] [nvarchar](10) NOT NULL,
	[Store Group Code] [nvarchar](10) NOT NULL,
	[Location Code] [nvarchar](10) NOT NULL,
	[Starting Date] [datetime] NOT NULL,
	[Status Code] [nvarchar](10) NOT NULL,
	[Comment] [nvarchar](50) NOT NULL,
	[Block Sale on POS] [tinyint] NOT NULL,
	[Block Purchasing] [tinyint] NOT NULL,
	[Block Transferring] [tinyint] NOT NULL,
	[Created by] [nvarchar](50) NOT NULL,
	[Created Date and Time] [datetime] NOT NULL,
	[Block Discount] [tinyint] NOT NULL,
	[Block Promotion Price] [tinyint] NOT NULL,
	[Block Periodic Discount] [tinyint] NOT NULL,
	[Block Manual Price Change] [tinyint] NOT NULL,
	[Block Sale in Sales Order] [tinyint] NOT NULL,
	[Block Sales Return] [tinyint] NOT NULL,
	[Block Negative Adjustment] [tinyint] NOT NULL,
	[Block Positive Adjustment] [tinyint] NOT NULL,
	[Block Purchase Return] [tinyint] NOT NULL,
	[Block From Recommendation] [tinyint] NOT NULL,
	[$systemId] [uniqueidentifier] NOT NULL,
 CONSTRAINT ['+@Company+'$Item Status Link$5ecfc871-5d82-43f1-9c54-59685e82318d$Key1] PRIMARY KEY CLUSTERED 
(
	[Item No_] ASC,
	[Variant Dimension 1 Code] ASC,
	[Variant Code] ASC,
	[Store Group Code] ASC,
	[Location Code] ASC,
	[Starting Date] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT ['+@Company+'$Item Status Link$5ecfc871-5d82-43f1-9c54-59685e82318d$$systemId] UNIQUE NONCLUSTERED 
(
	[$systemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]'

EXEC(@CreateTable)

END